﻿using System;
using System.Web;

namespace OrderingPlatform.MvcExtension
{
    public class DataCache
    {
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <returns></returns>
        public static string GetCache(string cacheKey, string cataLog = null)
        {
            if (!string.IsNullOrEmpty(cataLog))
            {
                cacheKey = cataLog + "." + cacheKey;
            }
            var objCache = HttpRuntime.Cache;
            return objCache[cacheKey] == null ? "" : objCache[cacheKey].ToString();
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        public static void SetCache(string cacheKey, string cacheValue)
        {
            SetCache(cacheKey, cacheValue, 60, null);
        }
        public static void SetCache(string cacheKey, string cacheValue, int expiry)
        {
            SetCache(cacheKey, cacheValue, expiry, null);
        }


        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="expiry"></param>
        /// <param name="cataLog"></param>
        public static void SetCache(string cacheKey, object objObject, int expiry, string cataLog)
        {
            SetCache(cacheKey, objObject, DateTime.Now.AddMinutes(expiry), TimeSpan.Zero, cataLog);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        /// <param name="cataLog"></param>
        public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration, string cataLog)
        {
            if (!string.IsNullOrEmpty(cataLog))
            {
                cacheKey = cataLog + "." + cacheKey;
            }
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 清空所有缓存
        /// </summary>
        public static void ClearAll(string cataLog = null)
        {

            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            if (cache.Count < 1) return;
            var cacheEnum = cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (string.IsNullOrEmpty(cataLog))
                {
                    RemoveItemFromCache(cacheEnum.Key.ToString());
                }
                else
                {
                    if (cacheEnum.Key.ToString().StartsWith(cataLog + "."))
                    {
                        RemoveItemFromCache(cacheEnum.Key.ToString());
                    }
                }

            }
        }

        /// <summary>
        /// 清空缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cataLog"></param>
        public static void RemoveItemFromCache(string key, string cataLog = null)
        {
            var cache = HttpRuntime.Cache;
            if (!string.IsNullOrEmpty(cataLog))
            {
                key = cataLog + "." + key;
            }

            if (cache[key] != null)
            {
                cache.Remove(key);
            }
        }
    }
}
